package com.societyGames.flashForms
{
import com.societyGames.system.EventObject;

import flash.display.DisplayObject;
import flash.events.Event;

public class ExpanderContainer extends SingleItemContainer implements IChangeSize
{
  private var _expanderHeaderContainer:ExpanderHeaderContainer;
  private var _sizeChangedEventObject:EventObject;
  private var _desiredWidth:Number;
  private var _desiredHeight:Number;

  public function ExpanderContainer(collapsed:DisplayObject, expanded:DisplayObject, collapsedOver:DisplayObject = null, expandedOver:DisplayObject = null)
  {
    this._sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED", Event);
    this._expanderHeaderContainer = new ExpanderHeaderContainer(collapsed, expanded, collapsedOver, expandedOver);
    this._expanderHeaderContainer.sizeChangedEventObject.addEventListener(expanderHeaderContainerSizeChangedHandler);
    this._expanderHeaderContainer.isExpandedChangedEventObject.addEventListener(expanderHeaderContainerIsExpandedHandler);
    this.addChild(this._expanderHeaderContainer);
  }

  private function expanderHeaderContainerSizeChangedHandler(event:Event):void
  {
    if (this._item != null)
    {
      this._item.y = this._expanderHeaderContainer.height;
    }
    this._sizeChangedEventObject.dispatchEvent();
  }

  private function expanderHeaderContainerIsExpandedHandler(event:Event):void
  {
    calculateIsExpanded();
  }

  private function calculateIsExpanded():void
  {
    this._item.visible = this._expanderHeaderContainer.isExpanded;
    this._sizeChangedEventObject.dispatchEvent();
  }

  override public function set item(value:DisplayObject):void
  {
    super.item = value;
    this._item.y = this._expanderHeaderContainer.height;
  }

  override protected function childSizeChangedHandler(event:Event):void
  {
    calculateIsExpanded();
  }

  public function get headerChild():DisplayObject
  {
    return this._expanderHeaderContainer.item;
  }

  public function set headerChild(value:DisplayObject):void
  {
    this._expanderHeaderContainer.item = value;
  }

  public function get isExpanded():Boolean
  {
    return this._expanderHeaderContainer.isExpanded;
  }

  public function set isExpanded(value:Boolean):void
  {
    this._expanderHeaderContainer.isExpanded = value;
  }

  public function get sizeChangedEventObject():EventObject
  {
    return this._sizeChangedEventObject;
  }

  public override function set width(value:Number):void
  {
    if (value != this._desiredWidth)
    {
      this._desiredWidth = value;
      this._expanderHeaderContainer.width = value;
      if (this._item != null)
      {
        this._item.width = this._desiredWidth;
      }
    }
  }

  public override function set height(value:Number):void
  {
    if (value != this._desiredHeight)
    {
      this._desiredHeight = value;
      calculateHeight();
    }
  }

  private function calculateHeight():void
  {
    //Set scale to default.
    if (this._desiredHeight > this._expanderHeaderContainer.desiredHeight)
    {
      this._expanderHeaderContainer.height = this._expanderHeaderContainer.desiredHeight;
      if (this._item != null)
      {
        this._item.height = this._desiredHeight - this._expanderHeaderContainer.height;
      }
    }
    else //We don't have enough room to display child.
    {
      this._expanderHeaderContainer.height = this._desiredHeight; //Shrink expander.
      this._item.height = 0; //Don't give any room to child.
    }
  }
}
}